package com.test.thread;

/**
 * Created with IntelliJ IDEA.
 * User: lxf
 * Date: 2016/8/30
 * Time: 14:14
 * To change this template use Editor | File and Code Templates.
 */
public class Product extends Thread {

    public static int bread = 0;
    private Object lock;

    public Product(Object l) {
        lock = l;
    }

    @Override
    public void run() {

        while (true) {
            try {
                synchronized (lock) {
                    if (bread < 10) {
                        bread++;
                        System.out.println("★★★生产了一个面包,当前面包数量: " + bread);
                        Thread.sleep(1000);
                        if (bread > 5) {
                            lock.notifyAll();
                            lock.wait();
                        }
                    } else {
                        System.out.println("停止生产面包,面包库已满!");
                        lock.wait();
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }


    public static void main(String[] args) {
        Object lock = new Object();
        Product product = new Product(lock);
        product.setName("A");
        product.start();
        Sale sale = new Sale(lock);
        sale.setName("B");
        sale.start();
    }

}
